﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LeadExpress.Models.ViewPages;

namespace LeadExpress.Areas.Management.Controllers
{
    public class BannerOptionListController : Controller
    {
        // Create an instance of user manage
        LeadExpress.Repository.BannerOptionManagementRepository repository = new LeadExpress.Repository.BannerOptionManagementRepository();

        //
        // GET: /Management/BannerOptionList/
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Index()
        {
            BannerOptionManagementModel model = new BannerOptionManagementModel();
            model = repository.GetModel(Helpers.Constant.HOME, "SuperAdministrator", 10, 1, 0);

            return View(model);
        }

        //
        // GET: /Management/BannerOptionList/Edit/id
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Add()
        {
            BannerOptionManagementModel model = new BannerOptionManagementModel();
            model = repository.GetModel(Helpers.Constant.HOME, "SuperAdministrator");

            return View(model);
        }

        [HttpPost]
        [ValidateInput(false)]
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Add(FormCollection form)
        {
            BannerOption model = new BannerOption();

            model.GroupID = Convert.ToInt32(form["BannerGroup"].ToString());
            model.PositionID = Convert.ToInt32(form["BannerPosition"].ToString());
            model.Name = form["Name"].ToString();
            model.ShortDesc = form["ShortDesc"].ToString();
            model.LongDesc = form["LongDesc"].ToString();
            model.Price = Convert.ToDecimal(form["Price"].ToString());
            model.UnitPrice = form["UnitPrice"].ToString();
            model.LongTimeValue = Convert.ToInt32(form["LongTimeValue"].ToString());
            model.LongTimeUnit = form["LongTimeUnit"].ToString();
            model.IsDefault = (form["IsDefault"] != null && form["IsDefault"].ToString() == "on");
            model.Active = (form["Active"] != null && form["Active"].ToString() == "on");

            repository.Create(model);

            return RedirectToAction("Index", "BannerOptionList");
        }

        //
        // GET: /Management/BannerOptionList/Edit/id
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Edit(int id)
        {
            BannerOptionManagementModel model = new BannerOptionManagementModel();
            model = repository.GetModel(Helpers.Constant.HOME, "SuperAdministrator", id);
            ViewBag.BannerOptionId = id;

            return View(model);
        }

        //
        // GET: /Management/BannerOptionList/Edit/id
        [HttpPost]
        [ValidateInput(false)]
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Edit(int id, FormCollection form)
        {
            BannerOption model = new BannerOption();

            if (model != null)
            {
                model.ID = id;
                model.GroupID = Convert.ToInt32(form["BannerGroup"].ToString());
                model.PositionID = Convert.ToInt32(form["BannerPosition"].ToString());
                model.Name = form["Name"].ToString();
                model.ShortDesc = form["ShortDesc"].ToString();
                model.LongDesc = form["LongDesc"].ToString();
                model.Price = Convert.ToDecimal(form["Price"].ToString());
                model.UnitPrice = form["UnitPrice"].ToString();
                model.LongTimeValue = Convert.ToInt32(form["LongTimeValue"].ToString());
                model.LongTimeUnit = form["LongTimeUnit"].ToString();
                model.IsDefault = (form["IsDefault"] != null && form["IsDefault"].ToString() == "on");
                model.Active = (form["Active"] != null && form["Active"].ToString() == "on");

                repository.Save(model);
            }

            return RedirectToAction("Index", "BannerOptionList");
        }

        // Delete Email Item
        [HttpGet]
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Delete(int id)
        {
            repository.Delete(id);

            BannerOptionManagementModel model = new BannerOptionManagementModel();
            model = repository.GetModel(Helpers.Constant.HOME, "SuperAdministrator");

            return Json(model, JsonRequestBehavior.AllowGet);
        }

        #region AJAX Post

        [HttpPost]
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Fillter(string keywords, string groupId, string positionId, int pageSize, int pageIndex, int id)
        {
            BannerOptionManagementModel model = new BannerOptionManagementModel();

            // Parse data
            string[] groups = groupId.Split(',');
            string[] position = positionId.Split(',');

            model = repository.GetModel(Helpers.Constant.HOME, "SuperAdministrator", keywords, groups, position, pageSize, pageIndex, id);

            return Json(model);
        }
        #endregion
    }
}
